package com.example.demo.story.impl;

import com.example.demo.story.NetStoryServiceAbstract;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author TCJ
 * @create 2024/6/27
 */
public class BiQuGe34Impl extends NetStoryServiceAbstract {
	@Override
	public String getBaseUrl() {
		return "https://www.biquge34.net";
	}

	@Override
	public String getReadUrl() {
		return getBaseUrl() + "/book/194355/";
	}

	@Override
	public void analysisChapter() {
		Document html = super.getHtml(getReadUrl());

		Element select = html.body().getElementsByTag("select").get(0);
		Elements option = select.getElementsByTag("option");
		List<String> htmlUrl = new ArrayList<>();
		dto.setTitle("tunbing--");
		for (Element element : option) {
			String value = element.attr("value");
			htmlUrl.add(getBaseUrl() + value);
			System.out.println(value);
		}
		for (String item : htmlUrl) {
			Document itemHtml = super.getHtml(item);
			Element list = itemHtml.body().getElementsByTag("dl").get(0);
			assert list != null;
			Elements a = list.getElementsByTag("a");
			for (Element element : a) {
				super.dto.getChapter().put(element.text(), getReadUrl() +  element.attr("href"));
			}
		}

	}

	@Override
	public String getContent(String url) {
		StringBuilder sb = new StringBuilder();
		nextContent(url,sb);
		return sb.toString();
	}
	private void nextContent(String url,StringBuilder sb) {
		Document html = super.getHtml(url);
		sb.append(Objects.requireNonNull(html.body().getElementById("htmlContent")).text());
		Element nextUrl = html.body().getElementById("linkNext");
		assert nextUrl != null;
		if ("下一页".equals(nextUrl.text())){
			nextContent(getReadUrl() + nextUrl.attr("href"),sb);
		}

	}
}
